import {http} from "./http";
import {eAddressTypeEnum, iAddressSyncMeta} from "../../packages/components/$address/address.utils";
import {createPlainAddressService} from "../../packages";
import {iPlainAddressData} from "../../packages/components/createPlainAddressService";

export const address = createPlainAddressService({
  http,
  getAddress: async () => {
    const list = (await import('../data/address.json')).default;
    const format = (list: iPlainAddressData[]): iAddressSyncMeta[] => {
      return list.map(i => ({
        label: i.name,
        code: i.code,
        type: (Deep2Type as any)[i.deep],
        parentCode: i.parentCode,
        ...!i.children ? {} : { children: format(i.children) },
      }));
    };
    return format(list as any);
  }
});

const Deep2Type = {
  0: eAddressTypeEnum.province,
  1: eAddressTypeEnum.city,
  2: eAddressTypeEnum.district,
} as const;
